import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/family_details_request.dart';
import 'package:elephant_fish/http/request/has_family_request.dart';
import 'package:elephant_fish/model/family_head_entity.dart';
import 'package:elephant_fish/page/family_member_page.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:elephant_fish/util/size_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'family_application_list_page.dart';
import 'login_page.dart';

class FamilyManagerInNewPage extends StatefulWidget {
  final int id;
  const FamilyManagerInNewPage({Key key, this.id}) : super(key: key);

  @override
  _FamilyManagerInNewPageState createState() => _FamilyManagerInNewPageState();
}

class _FamilyManagerInNewPageState extends State<FamilyManagerInNewPage> {

  bool isCreator = false;
  FamilyHeadEntity familyHeadEntity;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadData();
    hasFamily();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xff140c3d),
      appBar: AppBar(
        backgroundColor: Color(0xff140c3d),
        centerTitle: true,
        leading: GestureDetector(
          child: Icon(
            Icons.arrow_back_ios,
            size: SizeUtil.px(32),
            color: Color(0xfff3f3f3),
          ),
          onTap: () {
            Navigator.pop(context);
          },
        ),
        title: Text(
          '家族主页',
          style: TextStyle(fontSize: SizeUtil.px(32), color: Color(0xfff5f5f5)),
        ),
      ),
      body: Column(
          children: [
            SizedBox(height: 60,),
            Align(
              alignment: Alignment.topCenter,
              child: Container(
                width: 150,
                height: 150,
                child: CircleAvatar(
                  radius: SizeUtil.px(60),
                  backgroundImage: NetworkImage(
                      familyHeadEntity != null ?
                      'http://119.28.131.94:82' + familyHeadEntity.icon :
                      'https://img2.baidu.com/it/u=2860640912,1833965317&fm=26&fmt=auto&gp=0.jpg'
                  ),
                ),
              ),
            ),
            SizedBox(height: 20,),
            Text( familyHeadEntity != null ? familyHeadEntity.name : '家族1',style: TextStyle(color: Color(0xfff3f3f3),fontSize: 18),),
            SizedBox(height: 15,),
            Text( familyHeadEntity != null ? '族长:' + familyHeadEntity.headNickname : '族长:张三',style: TextStyle(color: Color(0x68f3f3f3),fontSize: 14),),
            SizedBox(height: 15,),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  width: 40,
                  height: 14,
                  color: Color(0xff00ccf9),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(Icons.person,size: 10,color: Color(0xffffffff),),
                      SizedBox(width: 3,),
                      Text( familyHeadEntity != null ? familyHeadEntity.persons.toString() : '10',style: TextStyle(color: Color(0xfff3f3f3),fontSize: 12),),
                    ],
                  ),
                ),
                SizedBox(width: 8,),
                Container(
                  width: 84,
                  height: 14,
                  color: Color(0xffc972ff),
                  child: Center(
                    child:Text( familyHeadEntity != null ? 'ID:' +  familyHeadEntity.idNumber : 'ID:60952354',style: TextStyle(color: Color(0xfff3f3f3),fontSize: 12),),
                  )
                ),
              ],
            ),
            SizedBox(height: 15,),
            Text( familyHeadEntity != null ? familyHeadEntity.introduction : '相亲相爱一家人，欢迎您的加入，来和我们 一起玩吧！',style: TextStyle(color: Color(0x68f3f3f3),fontSize: 14),),
            SizedBox(height: 70,),
            isCreator ?
            InkWell(
              child:  Container(
                width: 258.5,
                height: 36.5,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(21.5)),
                  color: Color(0xffe62d6c),
                ),
                child: Center(
                  child:Text( '家族成员',style: TextStyle(color: Color(0xfff3f3f3),fontSize: 14),),
                ),
              ),
              onTap: (){
                Navigator.of(context).push(MaterialPageRoute(
                        builder: (context) => FamilyMemberPage()));
                  },
            ) : Container(width: 0,height: 0,),
            SizedBox(height: 25,),
            isCreator ?
            InkWell(
              child:  Container(
                width: 258.5,
                height: 36.5,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(21.5)),
                  color: Color(0xfff3f3f3),
                ),
                child: Center(
                  child:Text( '成员申请',style: TextStyle(color: Color(0xffe62d6c),fontSize: 14),),
                ),
              ),
              onTap: (){
                Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => FamilyApplicationListPage()));
              },
            ) : Container(width: 0,height: 0,),

          ],
        ),
    );
  }

  Future<void> hasFamily() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    BaseRequest request = HasFamilyRequest();
    request.addHeader('usertoken', appToken);
    var result = await HiNet.getInstance().fire(request);
    if (result['code'] == -1) {
      TxUtils.showErrorToast('登录失效，请重新登录', context);
      Navigator.pop(context);
      Navigator.of(context).push(MaterialPageRoute(
        builder: (context) {
          return LoginPage(arguments: {'loginCode': '-1'},);
        },
      ));
    }else if(result['code'] == 0){

      setState(() {
        isCreator = result['data']['isCreator'];
      });

    }
  }

  Future<void> loadData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    BaseRequest request = FamilyDetailsRequest();
    request.addHeader('usertoken', appToken);
    var result = await HiNet.getInstance().fire(request);
    if (result['code'] == -1) {
      TxUtils.showErrorToast('登录失效，请重新登录', context);
      Navigator.pop(context);
      Navigator.of(context).push(MaterialPageRoute(
        builder: (context) {
          return LoginPage(arguments: {'loginCode': '-1'},);
        },
      ));
    }else if(result['code'] == 0){

      setState(() {
        familyHeadEntity = FamilyHeadEntity.fromJson(
            new Map<String, dynamic>.from(result['data']));
      });

    }
  }
}
